package com.wanxi.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanxi.dao.TeacherDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.Teacher;
import com.wanxi.service.TeacherService;
import com.wanxi.util.CreateOnlyID;
import com.wanxi.util.JsonCopy;
import com.wanxi.vo.TeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    TeacherDao teacherDao;
    @Autowired
    CreateOnlyID createOnlyId;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public ResultDTO findAllTeacher() {
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<Teacher>()
                .orderByDesc("id");
        List<Teacher> list=teacherDao.selectList(queryWrapper);
        if(list.size()>0){
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findTeacherById(TeacherVO teacherVO) {
        Teacher teacher= JsonCopy.copy(teacherVO, Teacher.class);
        Teacher t=teacherDao.selectById(teacher.getId());
        if(t!=null){
            return ResultDTO.getDTO(1,t);
        }
        return ResultDTO.builder().count(0).msg("error").code(1).build();
    }

    @Override
    public ResultDTO updateEnable(TeacherVO teacherVO) {
        Teacher teacher= JsonCopy.copy(teacherVO, Teacher.class);
        teacher.setUpdateTime(new Date());
        int i=teacherDao.updateById(teacher);
        if(i==1){
            stringRedisTemplate.delete("teacher");
            return ResultDTO.getDTO(i,teacherVO);
        }
        return ResultDTO.builder().data(teacherVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO addTeacher(TeacherVO teacherVO) {
        Teacher teacher= JsonCopy.copy(teacherVO,Teacher.class);
        teacher.setUpdateTime(new Date());
        teacher.setCreateTime(new Date());
        teacher.setId(createOnlyId.createId());
        int i=teacherDao.insert(teacher);
        if(i==1){
            stringRedisTemplate.delete("teacher");
            return ResultDTO.getDTO(i,teacherVO);
        }
        return ResultDTO.builder().data(teacherVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO update(TeacherVO teacherVO) {
        Teacher teacher=JsonCopy.copy(teacherVO, Teacher.class);
        teacher.setUpdateTime(new Date());
        int i=teacherDao.updateById(teacher);
        if(i==1){
            stringRedisTemplate.delete("teacher");
            return ResultDTO.getDTO(i,teacherVO);
        }
        return ResultDTO.builder().data(teacherVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO delete(TeacherVO teacherVO) {
        Teacher teacher=JsonCopy.copy(teacherVO, Teacher.class);
        int i=teacherDao.deleteById(teacher);
        if(i==1){
            stringRedisTemplate.delete("teacher");
            return ResultDTO.getDTO(i,teacherVO);
        }
        return ResultDTO.builder().data(teacherVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO findTeacher() {
        Long len = stringRedisTemplate.opsForList().size("teacher");
//        判断redis中是否有数据
        if(len!=0){
//            返回redis中的数据,通过fastJson转换
            List<String> redisTeacher = stringRedisTemplate.opsForList().range("teacher",0,len);
            List<Teacher> list= JSON.parseObject(redisTeacher.toString(), new TypeReference<List<Teacher>>() {});
            return ResultDTO.getDTO(list.size(),list);
        }
//        没有则从数据库中找数据
        List<Teacher> list=teacherDao.selectList(null);
        if(list.size()>0){
            Iterator<Teacher> iterator=list.iterator();
            while (iterator.hasNext()){
                Teacher t= iterator.next();
                if (t.getEnable()==0) {
                    iterator.remove();
                }
                else{
//                    将查到的数据放入redis
                    stringRedisTemplate.expire("teacher",300, TimeUnit.SECONDS);
                    stringRedisTemplate.opsForList().rightPush("teacher", JSON.toJSONString(t));
                }
            }
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }
}
